<?php
declare (strict_types = 1);

namespace app\controller;

use app\mine\jwtService;
use think\Request;

class UserController
{

    protected $usermodel;
    protected $yzm;
    public function __construct(){
//        这里是验证码
        $this->yzm = new CaptchaController();
        $this->usermodel = new \app\model\User();
    }

    //    后端的登陆方法
    public  function login(Request $request){
        $email = $request->post('email');
        $password = $request->post('password');

        $jwtService = new jwtService();
        $token = $jwtService->generateToken(['email' => $email], 'your_secret_key');

//        在已经登陆的时候修改登陆的状态，
//        如果已经登陆提示用户在别处登陆
        // 查询用户是否存在
        $user = $this->usermodel->where('email', $email)->find();
        $res = $this->usermodel->where('email', $email)->field(["username","headPortrait","role_id","email"])->find();
        if (!$user) {
            // 用户不存在
            return json(['message' => '邮箱不存在',"code"=>200]);
        }else{
            if (password_verify($password, $user['password'])) {
                // 密码正确
                return json(['message' => '登录成功', "code" => 200, "data" => ["token" => $token,"data" => $res]]);
            } else {
                // 密码错误
                return json(['message' => '密码错误',"code"=>400]);
            }
        }

    }


//    注册
    public function register(Request $request){
        $email = $request->post('email');
        $password = $request->post('password');
        $username = $request->post('username');
        $captcha = $request->post('captcha');

//        验证码不一致
        if ($this->yzm->verify($captcha)) {
            return json([
                "code" => 400,
                "message" => "验证码错误",
                "data" => null
            ]);
        }

        //对密码进行加密
        $newPassword = password_hash($password, PASSWORD_DEFAULT);

        $user = $this->usermodel->where('email', $email)->find();
        if ($user) {
            // 用户已存在
            return json([
                "message"=>"邮箱已存在，请重新确认！",
                "code"=>400
            ]);
        }else{
//            如果邮箱不存在则可以注册
            try {
                $res = $this->usermodel::create(data: [
                    'username' => $username,
                    'password' => $newPassword,
                    'email' => $email
                ]);
            }catch (\Exception $e){
                return json([
                    "code" =>404,
                    "message" => $e->getMessage()
                ]);
            }
            if($res){
                return json([
                    "code"=>200,
                    "message"=>"注册成功!",
                    "data"=>null
                ]);
            }else{
                return json([
                    "code"=>400,
                    "message"=>"注册失败!"
                ]);
            }

        }
    }

    // 退出登录
    public function logout(){
        return json(['message' => '注销成功', 'code' => 200,"data"=>null]);
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
